package com.ait.cms.controller;

import com.ait.cms.entity.ConstantWxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller
@RequestMapping("/api/wx")
public class WxApiController {

    @GetMapping("login")
    public String login(){
        System.out.println("==========================================");
        // 微信开放平台授权baseUrl
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=%s" +
                "&redirect_uri=%s" +
                "&response_type=code" +
                "&scope=snsapi_login" +
                "&state=%s" +
                "#wechat_redirect";

        String wxOpenRedirectUrl = ConstantWxUtils.WX_OPEN_REDIRECT_URL;
        try {
            wxOpenRedirectUrl = URLEncoder.encode(wxOpenRedirectUrl,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String Url = String.format(baseUrl, ConstantWxUtils.WX_OPEN_APP_ID, wxOpenRedirectUrl,"atguigu");
        return "redirect:"+Url;
    }

}
